<template>
    <div class="cmt-container">
        <h3>发表评论</h3>
        <hr>
        <textarea placeholder="请输入评论内容(最多输入120字)" maxlength="120" v-model="content"></textarea>
        <mt-button type="primary" size="large" @click="publish">发表评论</mt-button>
        <div class="cmt-list" v-for="(item,i) in comments" :key="item.id">
            <div class="cmt-item">
                <div class="cmt-title">
                    第{{i+1}}楼&nbsp;&nbsp;用户:匿名用户&nbsp;&nbsp;发表时间:{{item.createDate|dataFormat("YYYY年MM月DD日 HH:mm:ss")}}
                </div>
                <div class="cmt-body">
                    {{item.content}}
                </div>
            </div>
        </div>
        <mt-button type="danger" size="large" plain @click="getmore">加载更多</mt-button>
    </div>
</template>
<style lang="scss" scoped>
.cmt-container{
    h3{
        font-size: 16px;
    }
    textarea{
        font-size: 14px;
        height: 85px;
        margin: 0px;
    }
    .cmt-list{
         margin: 5px 0;
        .cmt-item{
            font-size: 13px;
            .cmt-title{
                line-height: 35px;
                background-color: #ccc;
                text-indent: 2em;
            }
            .cmt-body{
                
            }
        }
    }
}
</style>
<script>
import {Toast} from "mint-ui"
export default {
    data(){
        return {
            comments:[],
            page:1,
            size:5,
            content:""
        }
    },
    created(){
        this.getComments();
    },
    props:['nid'],
    methods:{
        getComments(){
            console.log(this.nid)
            this.$http.get("getComments?size="+this.size+"&page="+this.page+"&newsid="+this.nid).then(function(res){
                this.comments=this.comments.concat(res.body.data.records)
            })
        },
        getmore(){
            this.page++;
            this.getComments();
        },
        publish(){
            if(this.content.trim().length===0){
                return Toast("评论内容不能为空");
            }
            this.$http.get("saveComments?newsid="+this.nid+"&content="+this.content.trim()).then(res=>{
                this.comments.unshift(res.body.data)
                this.content=""
            })
        }
    }
}
</script>